package Machine;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;

import experiments.*;
import system.*;

public class PhysicalMachine {
	private String hostIP;
	private String hostName;
	private ArrayList<VirtualMachine> VMs=new ArrayList<VirtualMachine>();
	
	public PhysicalMachine(String hostIP, String hostName){
		this.hostIP=hostIP;
		this.hostName=hostName;
		singleLevelConverge.addPM(this);
	}
	
	public void addVM(VirtualMachine VM){
		VMs.add(VM);
	}
	public void removeVM(VirtualMachine VM){
		VMs.remove(VM);
	}
	
	public String getHostIP(){
		return hostIP;
	}
	
	public String getHostName(){
		return hostName;
	}
	
	public void assignCPU(){
		openURL("http://"+this.hostIP+":9111/vcontrol/vm/"+VMs.get(0).getVMName()+"/cpu/share/"+(int)((1-VMs.get(0).getCPUIFReq())*1000));
		openURL("http://"+this.hostIP+":9111/vcontrol/vm/"+VMs.get(1).getVMName()+"/cpu/share/"+(int)((VMs.get(0).getCPUIFReq())*1000));
		
	}
	
	private static String openURL(String urlString){
		String ret = null;

		try{
			URL url = new URL(urlString);
			BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

			ret = br.readLine();
		}
		catch(Exception e){
			e.printStackTrace();
		}

		return ret;
	}
	
}
